var phone = "";
var code = "";
var contextPath;
var oldphone=$("#oldphone").val();
$(function(){
	basePath = $(".basePath").attr("url");
	if(basePath == undefined){
		basePath = $("#contextPath").val();
	}
	 contextPath = $("#contextPath").val();
	$("#phone").blur(function(){
		phone=this.value;
		phone=$.trim(phone);
	    if(phone!=""&&!/^(13[0-9]|17[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(phone)){
			layer.open({
			    content:'手机号码格式不正确！',
			    time: 2
			});
			return false;
		}else if(phone==oldphone){
			layer.open({
			    content:'新手机号不能与原手机号相同！',
			    time: 2
			});
			$("#phone").val("");
			return false;
		}
	    else{
	    	$.ajax({
	    		url:"checkphone",
	    		type:"post",
	    		data:{"phone":phone},
	    		success:function(d){
	    			if(d.result.message=='failed'){
	    				layer.open({
	    				    content:'此手机号已被注册！',
	    				    time: 2
	    				});
	    				$("#phone").val("");
	    			}else if(d.result.message=='error'){
	    				layer.open({
	    				    content:'系统故障，紧急处理中！',
	    				    time: 2
	    				});
	    			}
	    		}
	    	});
	    }
	});
	$("#code").blur(function(){
		code=this.value;
		code=$.trim(code);
		if(code==""){
			layer.open({
			    content:'请填写验证码!',
			    time: 2
			});
		}
	});
});
function getCode(){
	var id;
	var types = $("#types").val();
	if($.trim(phone)==""){
		layer.open({
		    content:'请填写手机号',
		    time: 2
		});
		return false;
	}else if(!/^(13[0-9]|17[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(phone)){
		layer.open({
		    content:'手机号码格式不正确！',
		    time: 2
		});
		return false;
	}
	$.ajax({
		url:basePath+"/send",
		type:"post",
		data:{"userName":phone,"type":types},
		success:function(d){
			layer.open({
			    content:'您的验证码是:'+d.code,
			    time: 2
			});
			$("#codeHidden").val(d.code);
		}
	});
}
function submitPhone(){
	var codeHidden = $("#codeHidden").val();
	var phone = $("#phone").val();
	var code = $("#code").val();
	if($.trim(phone)==""){
		layer.open({
		    content:'请填写手机号',
		    time: 2
		});
		return false;
	}
	if($.trim(code)==""){
		layer.open({
		    content:'请填写验证码!',
		    time: 2
		});
		return false;
	}
	if($("#code").val() == codeHidden){
		$.ajax({
			url:"submitPhone",
			type:"post",
			data:{"phone":phone},
			success:function(data){
				layer.open({
				    content: data.result.message,
				    style: 'background-color:rgba(0,0,0,.8); color:#fff; border:none;',
				    time: 1,
				    shade: false 
				});
				$("#phone").val("");
				$("#code").val("");
				window.location.href="toSet";
			}
		});
	}else{
		layer.open({
		    content:'验证码不正确!',
		    time: 2
		});
	}
}
